package com.crazyktv.wcf.android;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import android.app.Activity;
import android.util.Log;

public class Util extends Activity {
	static String prefIP, prefPort, SERVICE_URI, prefRank;
	// static long prefRefresh;
	// static int prefLimitNum;
	// static boolean prefLimit;
	static boolean prefNumLimit;
	static int prefNumLen, prefNewSongInnerDays;
	static long prefRefreshTime;
	static int prefqueryRows = 1000;
	static int prefQueryRankRows = 100;
	static final int timeoutConnection = 3000;
	static final int timeoutSocket = 3000;
	private static final String orderSong = "OrderSong?value=";
	private static final String doAction = "DoCrazyKTV_Action?state=";
	private static final String doControl = "DoCrazyKTV_Control?state=";
	private static final String doValue = "&value=";
	private static final String querySinger = "QuerySinger?rows=500&sort=Singer_Name&condition=Singer_Type=";
	private static final String querySingerSong = "QuerySong?rows=500&sort=Song_WordCount,Song_SongName&condition=Song_Singer+like+";
	private static final String querySong = "QuerySong?condition=";
	private static final String queryRows = "&rows=";
	private static final String viewSong = "ViewSong?rows=100";
	private static final String favoriteUser = "FavoriteUser?rows=100";
	private static final String favoriteLogin = "FavoriteLogin?user=";
	private static final String favoriteSong = "FavoriteSong?user=";

	static enum DoAction {
		WAIT_DEL("Delete"), // 刪歌,value Song_Id
		WAIT_INSERT("InsertV"), // 待播清單插播,value 待播清單中的第幾筆
		INSERT("Insert"), // 歌曲查詢插播,value Song_Id
		CUT("Cut"), // 切歌,value 不需值
		RESET("RsetPlay"), // 重播,value 不需值
		REPLAY("Replay"), // 重覆,value 不需值
		PLAY("PlayPause"), // 播放暫停,value 不需值
		PLAY_FW("Forward"), // 快進,value 不需值
		PALY_BA("Back");// 快退,value 不需值

		private String doaction;

		DoAction(String doaction) {
			this.doaction = doaction;
		}

		public String getValue() {
			return doaction;
		}
	}

	static enum DoControl {
		VOL("Volume"), // 音量,value 1代表加 -1代表減
		MUTE("Mute"), // 靜音,value 不需值
		KEY("Pitch"), // 升降Key,value 1代表加 -1代表減
		KEY_ORI("DefaultPitch"), // 原Key,value 不需值
		KEY_MALE("MaleVoice"), // 男調,value 不需值
		KEY_FEMALE("WomanVoice"), // 女調,value 不需值
		SINGWITH("Channel"); // 聲道,value 不需值
		private String docontrol;

		DoControl(String docontrol) {
			this.docontrol = docontrol;
		}

		public String getValue() {
			return docontrol;
		}
	}

	static enum LangList {
		GUO("國語"), TAI("台語"), YUEI("粵語"), ENG("英語"), JPN("日語"), KER("客語"), KOR(
				"韓語"), OTHER("其它"), KID("兒歌");
		private String langlis;

		LangList(String langlis) {
			this.langlis = langlis;
		}

		public String getValue() {
			return langlis;
		}
	}

	static String QuerySinger(int value) {
		String uri = querySinger + value;
		return sendCode(uri);
	}

	static String QuerySingerSong(String value) {
		value = value.replace(" ", "%20");
		String uri = querySingerSong + "%27%2A" + value + "%2A%27";
		return sendCode(uri);
	}

	static String QuerySong(String value, int rows) {
		String uri = querySong + value + queryRows + rows;
		return sendCode(uri);
	}

	static String QuerySong(String value) {
		String uri = querySong + value + queryRows + prefqueryRows;
		
		return sendCode(uri);
	}

	static String OrderSong(String value) {
		String uri = orderSong + value;
		return sendCode(uri);
	}

	static String ViewSong() {
		String uri = viewSong;
		return sendCode(uri);
	}

	static String FavoriteUser() {
		String uri = favoriteUser;
		return sendCode(uri);
	}

	static String FavoriteSong(String value) {
		String uri = favoriteLogin + value;
		try {
			HttpGet request = new HttpGet(SERVICE_URI + uri);
			Log.d("sendCode", SERVICE_URI + uri);
			request.setHeader("Accept", "application/json");
			request.setHeader("Content-type", "application/json");
			DefaultHttpClient httpClient = new DefaultHttpClient(
					httpParameters());
			httpClient.execute(request);
		} catch (Exception e) {
			e.printStackTrace();
		}
		uri = favoriteSong + value + queryRows + prefqueryRows;
		return sendCode(uri);
	}

	static String DoAction(DoAction state, String value) {
		String uri = doAction + state.getValue() + doValue + value;
		Log.d("doAction", uri);
		return sendCode(uri);
	}

	static String DoControl(DoControl state, String value) {
		String uri = doControl + state.getValue();
		if (!value.equals("")) {
			uri = uri + doValue + value;
		}
		Log.d("doControl", uri);
		return sendCode(uri);
	}

	// set timeout
	static HttpParams httpParameters() {
		HttpParams hp = new BasicHttpParams();
		// Set the timeout in milliseconds until a connection is established.
		// The default value is zero, that means the timeout is not used.
		HttpConnectionParams.setConnectionTimeout(hp, timeoutConnection);
		// Set the default socket timeout (SO_TIMEOUT)
		// in milliseconds which is the timeout for waiting for data.
		HttpConnectionParams.setSoTimeout(hp, timeoutSocket);
		return hp;
	}

	@SuppressWarnings("finally")
	private static String sendCode(String uri) {
		String result = "";
		try {
			HttpGet request = new HttpGet(SERVICE_URI + uri);
			Log.d("sendCode", SERVICE_URI + uri);
			request.setHeader("Accept", "application/json");
			request.setHeader("Content-type", "application/json");

			DefaultHttpClient httpClient = new DefaultHttpClient(
					httpParameters());

			HttpResponse response = httpClient.execute(request);
			HttpEntity responseEntity = response.getEntity();
			Log.d("entity", String.valueOf(responseEntity.getContentLength()));
			// Read response data into buffer
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					response.getEntity().getContent(), "UTF-8"));
			result = reader.readLine();
			reader.close();
			Log.d("json", result);
		} catch (Exception e) {
			e.printStackTrace();
			result = "fail";
		} finally {
			return result;
		}
	}

}
